<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/common/taglibs.jsp"%>
<script type="text/javascript">
$(function() {
    parent.$.messager.progress('close');

    $('#form').form({
        url : contextPath+"/fobcost/editFobCost.action",
        onSubmit : function() {
            parent.$.messager.progress({
                title : '提示',
                text : '数据处理中，请稍后....'
            });
            //检查Form参数合法性,返回false不提交
           if(!checkForm()){
               parent.$.messager.progress('close');
                return false;
            }
        },
        success : function(data) {
            parent.$.messager.progress('close');
            data = JSON.parse(data);
            if(data && "success" == data.result){
           	 parent.$.messager.alert('提示信息', data.msg, 'info');
           	 parent.$.modalDialog.handler.dialog('close');
          	     parent.$.modalDialog.openner_dataGrid.datagrid('reload');
            } else {
                parent.$.messager.alert('错误', data.msg, 'error');
            }
        }
    });
});

function checkForm(){
	var isValid = true;
	var item = $("#form").find("[name='item']").validatebox({
	    required: true
	});
	isValid = isValid && item.validatebox('isValid');
	
	var fobSize = $("#form").find("[name='fobSize']").validatebox({
	    required: true
	});
	isValid = isValid && fobSize.validatebox('isValid');
	if(isValid){//判断规格格式，因为规格用于计算平方数,所以不能错
		var fobSizeVal = $.trim($("#fobSize").val());
		var fobSizeValArray = fobSizeVal.split("*");
		if(fobSizeValArray.length != 3){
			parent.$.messager.alert(var_alert_title, var_format_error, 'info');
			return false;
		}else{
			for(var i=0;i<3;i++){
				if(!isInteger(fobSizeValArray[i])){
					parent.$.messager.alert(var_alert_title, var_format_error, 'info');
					return false;
				}
			}
		}
	}
	
	//检查细表
	var itemEn = $("#form").find("[name='itemEn']").validatebox({
	    required: true
	});
	var itemZn = $("#form").find("[name='itemZn']").validatebox({
	    required: true
	});
	var isBaseonInvo = $("#form").find("[name='isBaseonInvo']").validatebox({
	    required: true
	});
	
	if(itemEn.length == 0){//针对细表记录为空情况
	 	   return isValid;
	}else{
		var itemEnVal = "";
		for(var i=0;i<itemEn.length;i++){//逐行检查空情况
			itemEnVal = $.trim(itemEn[i].value);
			if(isEmpty(itemEnVal)){
				isValid = false;
			}
		}
	}
	if(itemZn.length == 0){
	 	   return isValid;
	}else{
		var itemZnVal = "";
		for(var i=0;i<itemZn.length;i++){//逐行检查空情况
			itemZnVal = $.trim(itemZn[i].value);
			if(isEmpty(itemZnVal)){
				isValid = false;
			}
		}
	}
	if(isBaseonInvo.length == 0){
	 	   return isValid;
	}else{
		var isBaseonInvoVal = "";
		for(var i=0;i<isBaseonInvo.length;i++){//逐行检查空情况
			isBaseonInvoVal = $.trim(isBaseonInvo[i].value);
			if(isEmpty(isBaseonInvoVal)){
				isValid = false;
			}
		}
	}
	
	return isValid;
}
</script>
<!-- edit-dialog begin -->
<div class="edit-param-context">
   <div class="p5">
            <form id="form" name="form" action="" method="post">
                <table class="tablewidth1">
                	<tr>
                		<td align="right">
                			项目：
                		</td>
                		<td align="left">
                			<input id="fobCostId" name="id" type="hidden" value="${fobCost.id}"/>
                			<input id="item" name="item" class="span2" type="text" value="${fobCost.item }"/>
                		</td>
                		<td align="right">
                			规格：
                		</td>
                		<td align="left">
                			<input id="fobSize" name="fobSize" class="span2"  type="text" value="${fobCost.fobSize }"/>
                		</td>
                		<td align="right">
                			<font color="red"><strong>工厂交货价：</strong></font>
                		</td>
                		<td align="left">
                			<input id="facDeliPrice" class="span2" name="facDeliPrice" type="text" onblur="javascript:caculateFob();"/>
                		</td>
                	</tr>
                	<tr>
                		<td align="right">
                			<font color="red"><strong>码头交货价：</strong></font>
                		</td>
                		<td align="left">
                			<input id="dockDeliPrice" name="dockDeliPrice" class="span2"  type="text" value="" disabled="disabled"/>
                		</td>
                		<td align="right">
                			FOB费用：
                		</td>
                		<td align="left">
                			<input id="fobFee" name="fobFee" class="span2"  type="text" value="" disabled="disabled"/>
                		</td>
                		<td align="right">
                			FOB单价(¥)：
                		</td>
                		<td align="left">
                			<input id="fobPriceRmb" name="fobPriceRmb" class="span2"  type="text" value="" disabled="disabled"/>
                		</td>
                	</tr>
                	<tr>
                		<td align="right">
                			FOB单价($)：
                		</td>
                		<td align="left">
                			<input id="fobPriceUsd" name="fobPriceUsd" class="span2"  type="text" value="" disabled="disabled"/>
                		</td>
                		<td align="right">
                			FOB+1(¥)：
                		</td>
                		<td align="left">
                			<input id="fobOneRmb" name="fobOneRmb" class="span2"  type="text" value="" disabled="disabled"/>
                		</td>
                		<td align="right">
                			FOB+1($)：
                		</td>
                		<td align="left">
                			<input id="fobOneUsd" name="fobOneUsd" class="span2"  type="text" value="" disabled="disabled"/>
                		</td>
                	</tr>
                	<tr>
                		<td align="right">
                			<font color="red"><strong>平方/柜：</strong></font>
                		</td>
                		<td align="left">
                			<input id="mperCtainer" name="mperCtainer" class="span2"  type="text" value=""/>
                		</td>
                		<td align="right">
                			<font color="red"><strong>箱数/柜：</strong></font>
                		</td>
                		<td align="left">
                			<input id="boxPerCtainer" name="boxPerCtainer" class="span2"  type="text" value=""/>
                		</td>
                		<td align="right">
                			<font color="green"><strong>FOB费用/柜：</strong></font>
                		</td>
                		<td>
                			<input id="fobFeeCtainer" name="fobFeeCtainer" class="span2"  type="text" value="" disabled="disabled" />
                		</td>
                	</tr>
                	<tr>
                		<td align="right">
                			<font color="red"><strong>拖车费(¥)：</strong></font>
                		</td>
                		<td align="left">
                			<input id="trailerCharge" name="trailerCharge" class="span2"  type="text"/>
                		</td>
                		<td align="right">
                			<span></span>
                		</td>
                		<td align="left">
                			<span></span>
                		</td>
                		<td align="right">
                			<font color="red"><strong>柜数：</strong></font>
                		</td>
                		<td>
                			<input id="containers" name="containers" class="span2"  type="text" value=""/>
                		</td>
                	</tr>
                </table>
               
                <div class="fn-clear"></div>
                <div>
                	<font style="font-weight: bold;">明细：</font>
                	<a id="addDetail" class="easyui-linkbutton" iconCls="icon-add" plain="false" onclick="javascript:addRow();">添加</a>
                </div>
                <div class="ddtl1" align="center">
                	<table class="tabledtl1" id="fobdtlTabId">
                		<tr>
                			<td width="4%">编号</td>
                			<td width="30%">项目(英文)</td>
                			<td width="30%">项目(中文)</td>
                			<td width="13%">按票/柜</td>
                			<td width="15%">金额</td>
                			<td width="8%">操作</td>
                		</tr>
                		<c:forEach items="${fobCost.fobCostList }" var="fobcost" varStatus="status">
			                <tr>
	                			<td align="center">
		                			<span>${status.count}</span>
		                			<input id="fobcostValueIds${status.count}" name="fobcostValueIds" type="hidden" value="${fobcost.id }"/>
	                			</td>
	                			<td><input id="itemEn${status.count}" name="itemEn" type="text" style="width: 210px;" value="${fobcost.itemEn }"/></td>
	                			<td><input id="itemZn${status.count}" name="itemZn" type="text" style="width: 210px;" value="${fobcost.itemZn }"/></td>
	                			<td>
	                				<select id="isBaseonInvo${status.count}" name="isBaseonInvo" onchange="javascript:caculateFob();" style="height: 28px;line-height: 26px;width:80px;">
										<option value="">-请选择-</option>
										<option value="true" <c:if test="${fobcost.isBaseonInvo eq true}">selected="selected"</c:if>>票</option>
										<option value="false" <c:if test="${fobcost.isBaseonInvo eq false}">selected="selected"</c:if>>柜</option>
									</select>
								</td>
								<td><input id="amount${status.count}" name="amount" class="span2" type="text" style="width: 80px;" value=""/></td>
	                			<td><input id="delBtn${status.count}" name="delBtn" type="button" value="删除" onclick="javascript:delRow(this.parentElement.parentElement);"/></td>
                			</tr>
			            </c:forEach>
                	</table>
                </div>
            </form>
        </div>
</div> 
<!-- edit-dialog end -->

<script type="text/javascript">
var rowCount = '${fn:length(fobCost.fobCostList) + 1}';
function addRow(){
	$("#fobdtlTabId").append("<tr>"+
	"<td align='center'>"+rowCount+
	"<input id='fobcostValueIds"+rowCount+"' name='fobcostValueIds' type='hidden' value=''/></td>"+
	"<td><input id='itemEn"+rowCount+"' name='itemEn' type='text' style=\"width: 210px;\"/></td>"+
	"<td><input id='itemZn"+rowCount+"' name='itemZn' type='text' style=\"width: 210px;\"/></td>"+
	"<td><select id='isBaseonInvo"+rowCount+"' name='isBaseonInvo' onchange='javascript:caculateFob();' class='easyui-combobox span2' style='height: 28px;line-height: 26px;width:80px;'>"+
	"<option value=''>-请选择-</option><option value='true'>票</option><option value='false'>柜</option></select></td>"+
	"<td><input id='amount"+rowCount+"' name='amount' class='span2' type='text' style=\"width: 80px;\"/></td>"+
	"<td><input id='delBtn"+rowCount+"' name='delBtn' type='button' value='删除' onclick='javascript:delRow(this.parentElement.parentElement)'/></td>"+
	"</tr>");
	
	$('#amount'+rowCount).numberbox({
	    min:0,
	    precision:2,
	    prefix:'¥',
	    onChange:function(newValue,oldVlaue){
	    	caculateFob();
	    },
	    value:0
	});
	
	rowCount++;
}

function delRow(rowObj){
	rowObj.parentElement.deleteRow(rowObj.rowIndex);
	caculateFob();
}

$(function(){
	$('#facDeliPrice').numberbox({
	    min:0,
	    precision:2,
	    value:'${fobCost.facDeliPrice }',
	    onChange:function(newValue,oldVlaue){
	    	facDeliPrice = newValue;
	    	caculateFob();
	    },
	    prefix:'¥'
	});
	$('#trailerCharge').numberbox({
	    min:0,
	    precision:2,
	    value:'${fobCost.trailerCharge }',
	    onChange:function(newValue,oldVlaue){
	    	trailerCharge = newValue;
	    	caculateFob();
	    },
	    prefix:'¥'
	});
	$('#dockDeliPrice').numberbox({
	    min:0,
	    precision:2,
	    value:0,
	    value:'${fobCost.dockDeliPrice }',
	    prefix:'¥'
	});
	$('#fobFee').numberbox({
	    min:0,
	    precision:2,
	    value:'${fobCost.fobFee }',
	    prefix:'¥'
	});
	$('#fobPriceRmb').numberbox({
	    min:0,
	    precision:2,
	    value:'${fobCost.fobPriceRmb }',
	    prefix:'¥'
	});
	$('#fobPriceUsd').numberbox({
	    min:0,
	    precision:2,
	    value:'${fobCost.fobPriceUsd }',
	    prefix:'$'
	});
	$('#fobOneRmb').numberbox({
	    min:0,
	    precision:2,
	    value:'${fobCost.fobOneRmb }',
	    prefix:'¥'
	});
	$('#fobOneUsd').numberbox({
	    min:0,
	    precision:2,
	    value:'${fobCost.fobOneUsd }',
	    prefix:'$'
	});
	$('#mperCtainer').numberbox({
	    min:0,
	    precision:2,
	    onChange:function(newValue,oldVlaue){
	    	caculateFob();
	    },
	    value:'${fobCost.mperCtainer }'
	});
	$('#boxPerCtainer').numberbox({
	    min:0,
	    value:'${fobCost.boxPerCtainer }'
	});
	$('#fobFeeCtainer').numberbox({
	    min:0,
	    precision:2,
	    value:'${fobCost.fobFeeCtainer }',
	    prefix:'¥'
	});
	$('#containers').numberbox({
	    min:1,
	    precision:0,
	    value:'${fobCost.containers }',
	    onChange:function(newValue,oldVlaue){
	    	caculateFob();
	    }
	});
	
	//给细表赋值
	<c:forEach items="${fobCost.fobCostList }" var="fobcost1" varStatus="status1">
		$('#amount${status1.count}').numberbox({
		    min:0,
		    precision:2,
		    prefix:'¥',
		    value:'${fobcost1.amount }',
		    onChange:function(newValue,oldVlaue){
		    	caculateFob();
		    }
		});
	</c:forEach>
});

//自动计算fob报价
/**
 * 计算方式: 细表的的金额累加，如果是否按票选择的是“是”,则先用金额/柜数，再和其他项累加
 */
var facDeliPrice = '${fobCost.facDeliPrice }';//工厂交货价
var trailerCharge = '${fobCost.trailerCharge }';//拖车费

function caculateFob(){
	var amountArray = $("#form").find("[name='amount']");
	var isBaseonInvoArray = $("#form").find("[name='isBaseonInvo']");
	var containers = $("#containers").val();//柜数
	var mperCtainer = $("#mperCtainer").val();//平方/柜
	var boxPerCtainer = $("#boxPerCtainer").val();//箱数/柜
	
	var dockDeliPrice = 0.0;//码头交货价 
	var fobFee = 0.0;//fob费用
	var fobPriceRmb = 0.0; //fob 单价（人民币）
	var fobPriceUsd = 0.0; //fob 单价（美元）
	var fobOneRmb = 0.0; //fob+1 单价（人民币）
	var fobOneUsd = 0.0; //fob+1 单价（美元）
	var totalCtainer = 0.0;//每柜fob总价

	//当拖车费为0时，fob 单价（人民币） = 工厂交货价 + FOB费用   ,不为0时，fob 单价（人民币） = 工厂交货价 + 拖车费/（平方/柜）,此时码头交货价=fob 单价（人民币）
	if(trailerCharge == 0){
		//根据细表计费  每柜fob总价
		$.each(amountArray,function(index,obj){
			if(isBaseonInvoArray[index].value == 'true'){//按票计算
				totalCtainer += parseFloat($(obj).val())/containers;
			}else{
				totalCtainer += parseFloat($(obj).val());
			}
			
		});
		
		$('#fobFeeCtainer').numberbox({
		    min:0,
		    precision:2,
		    prefix:'¥',
		    value:totalCtainer
		});
		
		//计算fob费用   fob费用 = 每柜fob总价/每柜平方数
		if(!isEmpty(mperCtainer)){
			fobFee = totalCtainer/mperCtainer;
		}
		$('#fobFee').numberbox({
		    min:0,
		    precision:2,
		    prefix:'¥',
		    value:fobFee
		});
		
		//计算FOB单价(人民币)   FOB单价(人民币) = 工厂交货价+fob费用
		fobPriceRmb = parseFloat(facDeliPrice)+fobFee;
		$('#fobPriceRmb').numberbox({
		    min:0,
		    precision:2,
		    prefix:'¥',
		    value:fobPriceRmb
		});
		
		//将码头交货价置为0
		$('#dockDeliPrice').numberbox({
		    min:0,
		    precision:2,
		    prefix:'¥',
		    value:0
		});
	}else{//fob 单价（人民币） = 工厂交货价 + 拖车费/（平方/柜）,此时码头交货价=fob 单价（人民币）
		fobPriceRmb = parseFloat(facDeliPrice)+trailerCharge/mperCtainer;
		dockDeliPrice = fobPriceRmb;
		$('#fobPriceRmb').numberbox({
		    min:0,
		    precision:2,
		    prefix:'¥',
		    value:fobPriceRmb
		});
		$('#dockDeliPrice').numberbox({
		    min:0,
		    precision:2,
		    prefix:'¥',
		    value:dockDeliPrice
		});
		
		//将FOB费用，FOB费用/柜 置为0
		$('#fobFee').numberbox({
		    min:0,
		    precision:2,
		    prefix:'¥',
		    value:0
		});
		$('#fobFeeCtainer').numberbox({
		    min:0,
		    precision:2,
		    prefix:'¥',
		    value:0
		});
	}
	
	//计算FOB单价(美元)   FOB单价(美元) = 计算FOB单价(人民币)/系统设计的汇率
	fobPriceUsd = fobPriceRmb*parseFloat('${PARAMS_MAP["exchange_rate"]["USD"]}');
	$('#fobPriceUsd').numberbox({
	    min:0,
	    precision:2,
	    prefix:'$',
	    value:fobPriceUsd
	});
	
	//计算FOB+1单价(人民币)   FOB单价+1(人民币) = FOB单价(人民币) + 1.0
	fobOneRmb = fobPriceRmb + 1.0;
	$('#fobOneRmb').numberbox({
	    min:0,
	    precision:2,
	    prefix:'¥',
	    value:fobOneRmb
	});
	
	//计算FOB+1单价(美元)   FOB单价+1(美元) = FOB+1单价(人民币)/系统设计的汇率
	fobOneUsd = fobOneRmb*parseFloat('${PARAMS_MAP["exchange_rate"]["USD"]}');
	$('#fobOneUsd').numberbox({
	    min:0,
	    precision:2,
	    prefix:'$',
	    value:fobOneUsd
	});
}
</script>